package com.lun.easy;

import com.lun.util.BinaryTree.TreeNode;

public class SubtreeOfAnotherTree {
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
    	if(root == null ^ subRoot == null)
    		return false;
    	
        if(equals(root, subRoot))
        	return true;

    	return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
    }
    
    public boolean equals(TreeNode node1, TreeNode node2) {
    	if(node1 == null ^ node2 == null)
    		return false;
    	
    	if(node1 != null && node2 != null) {
    		if(node1.val != node2.val) 
    			return false;

    		return equals(node1.left, node2.left) && //
    				equals(node1.right, node2.right);
    	}
    	
    	return true;
    }
}
